<?php
namespace Modules\ShopApi\Models;
use Modules\AuthAdmin\Services\CommonService;
use Modules\ShopApi\Services\ShopUserService;
use Modules\ShopApi\Models\ShopPictureCollection as ShopPictureCollectionModel;
class ShopPicture extends BaseApiModel
{
	/**
	 * @name  关联商品图片   一对一
	 */
	 public function image_one()
    {
        return $this->hasOne('Modules\AuthAdmin\Models\AuthImage','id','image_id');
    } 
	/**
     * @name   更新时间为null时返回
     * @param  int  $value
     * @return Boolean 
     */
    public function getUpdatedAtAttribute($value)
    {
        return $value?$value:'';
    }
	/**
	 * @name  首页轮播图
	 * @return json 
	 */
    public function get_banner(){
        $baner_list = $this->select('id','url','image_id','the_jump')
				->with([
					'image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])
				->where(['type'=>1,'status'=>1])
				->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->get()
				->toArray();
		$pic_list = $this->select('id','url','image_id','the_jump','title') 
				->with([
					'image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])
				->where(['type'=>3,'status'=>1,'open'=>0])
				->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->get()  
				->toArray();			
		$baner_list = $this->common_image_one($baner_list);		
		$pic_list = $this->common_image_one($pic_list);		
		return $this->api_success(['data'=>['baner_list'=>$baner_list,'pic_list'=>$pic_list]]);		
	}
	/**
	 * @name  咨询轮播图
	 * @return json 
	 */
    public function get_consult_banner(){
        $list = $this->select('id','url','image_id','the_jump')
				->with([
					'image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])
				->where(['type'=>2,'status'=>1])
				->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->get()
				->toArray(); 
		$list = $this->common_image_one($list);		
		return $this->api_success(['data'=>$list]);		
	}
	/**
	 * @name  轮播图详情
	 * @return json 
	 */
    public function swiper_detail($id){
        $content = $this->where(['id'=>$id])->value('content'); 	
		return $this->api_success(['data'=>(new CommonService())->replace_pic_url($content)]);		
	}
	/**
	 * @name  直播详情
	 * @return json 
	 */
    public function pic_detail($id){
        $data = $this->select('id','content','url')->find($id);
		$user_info = (new ShopUserService())->my();
		$collection_status = 0;
		if($user_info){
			$collection_status = ShopPictureCollectionModel::where(['user_id'=>$user_info['id'],'picture_id'=>$id,'status'=>1])->count()?1:0;
		}
		return $this->api_success(['data'=>[
			'content' => (new CommonService())->replace_pic_url($data['content']),
			'url' => $data['url'],
			'collection_status' => $collection_status
		]]);		
	}
	/**
	 * @name  直播收藏
	 */
	public function picture_collection($id){
		$user_info = (new ShopUserService())->my();
		$collection_info = ShopPictureCollectionModel::where(['user_id'=>$user_info['id'],'picture_id'=>$id])->select('id','status')->first();
		$status = 1;
		if($collection_info){
			$collection_info = $collection_info->toArray();
			$status = $collection_info['status']==1?0:1;
			$res = ShopPictureCollectionModel::where(['id'=>$collection_info['id']])->update([
				'updated_at'=>date('Y-m-d H:i:s'),
				'created_at'=>date('Y-m-d H:i:s'),
				'status'=>$status
			]);
		}else{
			$res = ShopPictureCollectionModel::insert([
				'user_id'=>$user_info['id'],
				'picture_id'=>$id,
				'status'=>$status,
				'created_at'=>date('Y-m-d H:i:s')
			]);
		}
		if($res!==false){
			return $this->api_success(['msg'=>$status?'收藏成功！':'取消收藏成功！']);	
		}else{
			return $this->error();
		}
	}
	/**
	 * @name  往期直播列表
	 * @return json 
	 */
    public function get_live_list(){
		$list = $this->select('id','url','image_id','the_jump','title') 
				->with([
					'image_one'=>function($query){
						$query->select('id','url','open');  
					}
				])
				->where(['type'=>3,'status'=>1,'open'=>1])
				->orderBy('sort','asc') 
				->orderBy('id','desc') 
				->paginate(5)
				->toArray();				
		$list['data'] = $this->common_image_one($list['data']);
		return $this->api_success(['data'=>[
			'data'			=> $list['data'],
			'last_page'		=> $list['last_page'],
			'per_page'		=> $list['per_page'],
			'current_page'	=> $list['current_page'],
			'total' 		=> $list['total']
		]]);		
	}
}


